﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Account
{
    using System.Web.Security;
    using System.Web;
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Accounts;
    using SharingCenter.Web.Shared;
    using SharingCenter.Web.Classes.Message;

    public partial class Login : SharingCenter.Web.Classes.PageBase
    {
        bool validVericode = true;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                //string redir = "~/Home";
                //if (null != Request.QueryString["ReturnUrl"])
                //{
                //    redir = Request.QueryString["ReturnUrl"];
                //}
                
                //Response.Redirect(redir);
            }
            Login1.LoggedIn += new EventHandler(Login1_LoggedIn);
            Login1.LoginError+=new EventHandler(Login1_LoginError);
        }

        void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            
        }

        void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
        {
            if (!validVericode)
            {
                e.Cancel = true;
            }
        }

        void Login1_LoggedIn(object sender, EventArgs e)
        {
            //if (!RoleOperations.IsUserAdmin(Login1.UserName))
            //{
                

                
            //}
            //else
            //{
            //    Response.Redirect("~/admin");
            //}
            aspnet_User u = null;
            try
            {
                using (scDataContext cxt = new scDataContext())
                {
                    u = cxt.aspnet_Users.Single(d => d.UserName == Login1.UserName);
                }
            }
            catch
            {

            }

            if (u != null)
            {
                WebProfile p = null;
                try
                {
                    p = WebProfile.GetProfile(Login1.UserName);
                }
                catch { p = null; }

                if (null != p)
                {
                    p.UserId = u.UserId;
                    p.Security.IsSiteAdmin = RoleOperations.IsUserAdmin(Login1.UserName);
                    try
                    {
                        p.Save();
                    }
                    catch { }

                    if (p.Security.PasswordReset)
                    {
                        SetTopMostMessage("请立即修改密码。", true);
                        Response.Redirect("~/User/CurrentProfile/Security.aspx");
                    }
                    else if (!p.Website.ProfileUpdated)
                    {
                        SetTopMostMessage("完善一下你的个人资料吧。", true);
                        Response.Redirect("~/User/CurrentProfile");
                    }
                }
            }
        }

        protected void Login1_LoginError(object sender, EventArgs e)
        {
            SetErrorMessage("E-mail或密码错误，请重新输入。");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            //ValicationCode c = (ValicationCode)Login1.FindControl("ValidationCode1");
            if (!IsValid)
            {
                SetErrorMessage("验证码输入有误，请重新输入。");
                validVericode = false;
            }
            
        }

        void SetErrorMessage(string msg)
        {
            MessageBox box = (MessageBox)Login1.FindControl("msgLoginError");
            box.Text = msg;
            box.Visible = true;
            //ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "ReloadRecaptcha", "Recaptcha.reload();", true);
        }
    }
}
